package com.gsafety.wellsfort.common.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static JSONObject toJSONObject(String re){
		JSONObject jsStr = JSONObject.fromObject(re);
		System.out.println(jsStr);
		String data = jsStr.get("data").toString();
		System.out.println(data);
		//2、使用JSONArray
		JSONArray jsonArray=JSONArray.fromObject(data);
		//获得jsonArray的第一个元素
		Object o=jsonArray.get(0);
		return JSONObject.fromObject(o);

	}

	//大写字符转下划线加小写
	public static String camel4underline(String param){
		Pattern  p=Pattern.compile("[A-Z]");
		if(param==null ||param.equals("")){
			return "";
		}
		StringBuilder builder=new StringBuilder(param);
		Matcher mc=p.matcher(param);
		int i=0;
		while(mc.find()){
			builder.replace(mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase());
			i++;
		}

		if('_' == builder.charAt(0)){
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	// 判断手机号码格式是否正确
	public static boolean phoneMatch(String phone) {
		if (phone.matches("1[358][0-9]{9,9}")) {
			return true;
		}
		return false;
	}


	// 判断是否是股票代�?
	public static boolean stockMatch(String stock) {
		if (stock.matches("^(?i)s[hz]\\d{6}$")) {
			return true;
		}
		return false;
	}

	// 判断是否是否中文字符
	public static boolean char2Chinese(String context) {
		if (context.matches("[\u4e00-\u9fa5]")) {
			return true;
		}
		return false;
	}

	// 判断是否是否英文字符
	public static boolean char2English(String context) {
		if (context.matches("[a-zA-Z]")) {
			return true;
		}
		return false;
	}

	// 字符串中英文识别
	public static boolean isChinese(String strName) {
		int y = 0;
		int n = 0;
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (char2Chinese(String.valueOf(c)) == true) {
				y++;
			}
			if (char2English(String.valueOf(c)) == true) {
				n++;
			}
		}
		if (y >= n) {
			return true;
		} else {
			return false;
		}
	}

	// 提取引号内的内容
	public static List<String> getContentFromQuotes(String str) {
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile("\"(.*?)\"");
		Matcher m = p.matcher(str);
		while (m.find()) {
			list.add(m.group().replace("\"", ""));
		}
		return list;
	}

	// 提取小括号内的内�?
	public static List<String> getContentFromParentheses(String str) {
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile("\\(.*?\\)");
		Matcher m = p.matcher(str);
		while (m.find()) {
			list.add(m.group().replace("(", "").replace(")", ""));
		}
		return list;
	}

	// 提取中括号内的内�?
	public static List<String> getContentFromBraces(String str) {
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile("\\[(.*?)\\]");
		Matcher m = p.matcher(str);
		while (m.find()) {
			list.add(m.group().replace("[", "").replace("]", ""));
		}
		return list;
	}

	// 去掉�?后一位字�?
	public static String cutLastChars(String str, int num) {
		return str.substring(0, str.length() - num);
	}

	// 获取字符�?后出现的位置，并截取后半�?
	public static String cutLastPart(String str, String tag) {
		int pos = str.lastIndexOf(tag);
		return str.substring(pos + 1);
	}

	// 字符串为�?
	public static boolean isEmpty(String str) {
		if (str == null)
			return true;
		if ("".equals(str))
			return true;
		return false;
	}

	// 字符串不为空
	public static boolean isNotEmpty(String str) {
		if (str == null)
			return false;
		if ("".equals(str))
			return false;
		if("undefined".equals(str))
			return false;
		return true;
	}

	// 将null转换�?0
	public static double nvlDouble(String str) {
		double result;
		try {
			str = str.replaceAll("\"", "");
			result = Double.parseDouble(str);
		} catch (Exception e) {
			return 0.00;
		}
		return result;
	}

	public static int nvlInt(String str) {
		int result;
		try {
			str = str.replaceAll("\"", "");
			result = Integer.parseInt(str);
		} catch (Exception e) {
			return 0;
		}
		return result;
	}

	// 将null转换为空�?
	public static String nvlString(String str) {
		if (str == null)
			return "";
		return str;
	}

	// 对于java中出现的-0.0进行转换
	public static double getPositiveDouble(double param) {
		if (param == 0 && String.valueOf(param).startsWith("-"))
			return param * -1;
		else
			return param;
	}

	// 去掉�?后一位并将字符串转换为数�?
	public static String[] quarterSplit(String str, String split) {
		str = cutLastChars(str, 1);
		String[] arr = str.split(split);
		return arr;
	}

	// 判断�?个对象是否在数组�?
	public static boolean inArray(Object item, Object[] array) {
		if (array == null) {
			return false;
		}
		if (array.length == 0) {
			return false;
		}
		for (Object obj : array) {
			if (obj.equals(item)) {
				return true;
			}
		}
		return false;
	}

	public static boolean inList(String item, List<String> array) {
		if (array == null) {
			return false;
		}
		if (array.size() == 0) {
			return false;
		}
		for (Object obj : array) {
			if (obj.equals(item)) {
				return true;
			}
		}
		return false;
	}

	// 自动�?0
	public static String fillZero(int item, int count) {
		if (count < 1) {
			return null;
		}
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < count; i++) {
			buffer.append("0");
		}
		buffer.append(String.valueOf(item));
		return buffer.toString().substring(buffer.toString().length() - count);
	}



	/**
	 * @author lihui
	 * @DO:	将带有String泛型的List组和成一个String变量�?
	 * @param list �?要转化的List
	 * @param format  组合的规�?(以什么形式组�?)
	 * @return String
	 */
	public static String listToString (List<String> list,String format){
		StringBuilder str = new StringBuilder();
		if(list != null && list.size() > 0){
			for(int i = 0 ,size = list.size(); i < size ; i++){
				//此处做if判断是防止迭代到�?后一个数据的时�?�仍然在�?后添加了�?个format字符串�??
				if( (i+1) != size ){
					str.append(list.get(i)+format);
				}else{
					str.append(list.get(i));
				}
			}
		}
		return str.toString();
	}
}